#load "test_tk_token.cmo";;
#use "test_tk_parser.ml";;

module Token = Test_tk_token;;

let p = Calc.create ();;

let p = Calc.post 1 (Token.Number 1) p;;
let p = Calc.post 2 Token.Add p;;
let p = Calc.post 3 (Token.Number 2) p;;
let p = Calc.post 4 Token.Mul p;;
let p = Calc.post 5 (Token.Number 3) p;;
let p = Calc.post 6 Token.Eof p;;

let rec eval (x: int Calc.expr option) = (
	begin match x with
	| Some e -> 
		begin match e with
		| Calc.Identity_term t -> eval_term t
		| Calc.Makeadd (a, b) -> (eval a) + (eval_term b)
		| Calc.Makesub (a, b) -> (eval a) - (eval_term b)
		end
	| None -> min_int
	end
) and eval_term (x: int Calc.term option) = (
	begin match x with
	| Some e -> 
		begin match e with
		| Calc.Identity_number a -> eval_num a
		| Calc.Makemul (a, b) -> (eval_term a) * (eval_num b)
		| Calc.Makediv (a, b) -> (eval_term a) / (eval_num b)
		end
	| None -> min_int
	end
) and eval_num (x: Token.token option) = (
	begin match x with
	| Some (Token.Number value) -> value
	| Some _ | None -> min_int
	end
);;

if Calc.accepted p then (
	if eval (Calc.result p) = 7 then (
		begin match Calc.errors p with
		| [] ->
			print_string "OK.\n";
		| _ ->
			raise (Failure "BAD ERRORS DETECTED!!")
		end
	) else (
		raise (Failure "BAD RESULT!!")
	)
) else (
	raise (Failure "NOT ACCEPTED!!")
);;
